/*
 * $Id: MailreaderSupport.java 517026 2007-03-11 21:58:50Z husted $
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package trans.support;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.util.ServletContextAware;

import trans.action.support.PageMode;
import trans.action.support.PagerBean;
import trans.model.User;

import com.opensymphony.xwork2.ActionSupport;

public class BasicSupport extends ActionSupport implements ServletContextAware,
		ServletRequestAware,ServletResponseAware {

	private static final long serialVersionUID = 1L;

	private ServletContext context;

	protected HttpServletRequest request;
	
	protected HttpServletResponse response;
	
	protected PagerBean pager;

	public PagerBean getPager() {
		return pager;
	}

	public void setPager(PagerBean pager) {
		this.pager = pager;
	}

	protected Object getService(String name) {
		return context.getAttribute(name);
	}
	
	

	@Override
	public String execute() throws Exception {
		return super.execute();
	}

	protected PageMode getPageMode(HttpServletRequest request) {
		int offset = 0;
		if (request.getParameter("pager.offset") == null) {// pager.offset��taglib��ǩ�Դ������
			offset = 0;
		} else {
			offset = Integer.parseInt(request.getParameter("pager.offset"));
		}
		PageMode pm = new PageMode();
		pm.setOffset(offset);
		request.setAttribute("pm", pm);
		return pm;
	}
	
	protected void initPager(PageMode pm) {
		request.setAttribute("itemCount", pm.getTotal());
	}
	
	protected User currentUser(){
		return (User) request.getSession().getAttribute(Constants.USER_KEY);
	}
	
	protected void writeJsonObj(Object json) throws IOException {
		JSONObject JsonObject = JSONObject.fromObject(json);
	    response.setContentType("text/json;charset=utf-8");
	    response.getWriter().write(JsonObject.toString());
		response.getWriter().flush();
	}
	
	protected void writeJsonList(List<Object> jsonList) throws IOException {
		JSONArray ja = JSONArray.fromObject(jsonList);
	    response.setContentType("text/json;charset=utf-8");
	    response.getWriter().write(ja.toString());
		response.getWriter().flush();
	}

	@Override
	final public void setServletContext(ServletContext context) {
		this.context = context;
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}
	
	protected void initManuShow(){
	}
}
